let utils = require('utils');
let BlockScript = require('BlockScript');

cc.Class({
    extends: cc.Component,

    properties: {
        moveBlock:{
            type:cc.Node,
            default:null
        },
        allBlock:{
            type:BlockScript,
            default:[]
        },
        completeType: 1,
        deleteBar:{
            type:cc.Node,
            default:null
        },
        _magnetAo:{
            type:BlockScript,
            default:null
        },
        _magnetTu:{
            type:BlockScript,
            default:null
        },
        _aoDistance:0,
        _tuDistance:0,
        _complete:false,
        _delMoveBlock:false
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // this.node.on(cc.Node.EventType.MOUSE_DOWN, this.onMouseDown, this);
        this.node.on(cc.Node.EventType.MOUSE_MOVE, this.onMouseMove, this);
        this.node.on(cc.Node.EventType.MOUSE_UP, this.onMouseUp, this);
        // this.node.on(cc.Node.EventType.MOUSE_LEAVE, this.onMouseLeave, this);
    },

    start () {
    },

    // update (dt) {},

    onMouseMove(event) {
        if (this.moveBlock) {
            this.moveBlock.position = this.node.convertToNodeSpaceAR(event.getLocation());
            // cc.log(this.moveBlock.position);
            let bs = this.moveBlock.getComponent("BlockScript");
            if (this.deleteBar) {
                this._delMoveBlock = false;
                if (this.moveBlock.x < this.node.width/-2) {
                    let bstmp = bs;
                    this._delMoveBlock = bstmp.canDel;
                    while (bstmp.next && bstmp.canDel) {
                        if (bstmp.next.getComponent("BlockScript").canDel===false) {
                            this._delMoveBlock = false;
                            break;
                        }
                        bstmp = bstmp.next.getComponent("BlockScript");
                    }
                    this.deleteBar.active = true;
                    this.deleteBar.getComponent("DeleteBlockScript").setDelete(this._delMoveBlock);
                } else {
                    this.deleteBar.active = false;
                }
            }
            if (this._delMoveBlock) {
                return;
            }
            if (bs.ao) {
                let magnetAo =[];
                let movePos = bs.ao.convertToWorldSpaceAR(cc.Vec2.ZERO);
                for (let i = 0; i < this.allBlock.length; i++) {
                    if (this.allBlock[i] && this.allBlock[i]!=bs && this.allBlock[i].tu && bs.isChild(this.allBlock[i].node)===false) {
                        let tuws = this.allBlock[i].tu.convertToWorldSpaceAR(cc.Vec2.ZERO);
                        let distance = (movePos.x-tuws.x)*(movePos.x-tuws.x)+(movePos.y-tuws.y)*(movePos.y-tuws.y);
                        if (distance < 4000) {
                            magnetAo.push({dis:distance, block:this.allBlock[i]});
                        }
                    }
                }
                if (this._magnetAo) {
                    this._magnetAo.tuLine.active = false;
                    this._magnetAo = null;
                }
                if (magnetAo.length > 0) {
                    this._magnetAo = magnetAo[0].block;
                    this._aoDistance = magnetAo[0].dis;
                    for (let i = 1; i < magnetAo.length; i++) {
                        if (magnetAo[i].dis < this._aoDistance) {
                            this._magnetAo = magnetAo[i].block;
                            this._aoDistance = magnetAo[i].dis;
                        }
                    }
                    this._magnetAo.tuLine.active = true;
                    if (this._magnetTu) {
                        this._magnetTu.aoLine.active = false;
                        this._magnetTu = null;
                    }
                }
            }
            if (this._magnetAo===null) {
                // while (bs.next)
                // {
                //     bs = bs.next.getComponent("BlockScript");
                // }
                if (bs.tu && bs.next===null) {
                    let magnetTu =[];
                    let movePos = bs.tu.convertToWorldSpaceAR(cc.Vec2.ZERO);
                    for (let i = 0; i < this.allBlock.length; i++) {
                        if (this.allBlock[i] && this.allBlock[i]!=bs && this.allBlock[i].ao && bs.isChild(this.allBlock[i].node)===false) {
                            let aows = this.allBlock[i].ao.convertToWorldSpaceAR(cc.Vec2.ZERO);
                            let distance = (movePos.x-aows.x)*(movePos.x-aows.x)+(movePos.y-aows.y)*(movePos.y-aows.y);
                            if (distance < 4000) {
                                magnetTu.push({dis:distance, block:this.allBlock[i]});
                            }
                        }
                    }
                    if (this._magnetTu) {
                        this._magnetTu.aoLine.active = false;
                        this._magnetTu = null;
                    }
                    if (magnetTu.length > 0) {
                        this._magnetTu = magnetTu[0].block;
                        this._tuDistance = magnetTu[0].dis;
                        for (let i = 1; i < magnetTu.length; i++) {
                            if (magnetTu[i].dis < this._tuDistance) {
                                this._magnetTu = magnetTu[i].block;
                                this._tuDistance = magnetTu[i].dis;
                            }
                        }
                        this._magnetTu.aoLine.active = true;
                    }
                }
            }
        }
    },
    onMouseUp(event) {
        // if (this._magnetAo && this._magnetTu) {
        //     if (this._aoDistance < this._tuDistance) {
        //         this._magnetAo.attractAo(this.moveBlock);
        //     } else {
        //         this._magnetTu.attractTu(this.moveBlock);
        //     }
        // }
        if (this._magnetAo) {
            this._magnetAo.attractAo(this.moveBlock);
        }
        else if (this._magnetTu) {
            this._magnetTu.attractTu(this.moveBlock);
        }
        if (this.moveBlock) {
            if (this.deleteBar && this._delMoveBlock) {
                this.delMoveBlock();
            } else {
                if (this.moveBlock.x < this.node.width/-2+this.moveBlock.width/2) {
                    this.moveBlock.x = this.node.width/-2+this.moveBlock.width/2;
                }
                else if (this.moveBlock.x > this.node.width/2-this.moveBlock.width/2) {
                    this.moveBlock.x = this.node.width/2-this.moveBlock.width/2;
                }
                if (this.moveBlock.y < this.node.height/-2+this.moveBlock.height/2) {
                    this.moveBlock.y = this.node.height/-2+this.moveBlock.height/2;
                }
                else if (this.moveBlock.y > this.node.height/2-this.moveBlock.height/2) {
                    this.moveBlock.y = this.node.height/2-this.moveBlock.height/2;
                }
                this.completeCheck();
            }
        }
        if (this.deleteBar) {
            this.deleteBar.active = false;
        }
        this._magnetAo = null;
        this._magnetTu = null;
        this.setMoveBlock(null);
        for (let i = 0; i < this.allBlock.length; i++) {
            if (this.allBlock[i]) {
                if (this.allBlock[i].aoLine) {
                    this.allBlock[i].aoLine.active = false;
                }
                if (this.allBlock[i].tuLine) {
                    this.allBlock[i].tuLine.active = false;
                }
            }
        }
        // cc.log("onMouseUp");
    },
    // onMouseLeave(event) {
    //     if (this.moveBlock && this.moveBlock.getComponent("BlockScript")._isDown==false) {
    //         this.setMoveBlock(null);
    //         cc.log("onMouseLeave");
    //     }
    // },
    setMoveBlock(b) {
        if (b) {
            if (this.moveBlock==null) {
                this.moveBlock = b;
                this.moveBlock.zIndex = 10;
                b.getComponent("BlockScript").disattract();
            }
        } else {
            if (this.moveBlock) {
                this.moveBlock.zIndex = 1;
                this.moveBlock = null;
            }
        }
    },
    completeCheck() {
        if (this._complete==false) {
            let complete = true;
            switch (this.completeType) {
                case 0:
                    let b1 = this.allBlock[0].node;
                    let tag = cc.find("Canvas/work/b1-t1");
                    if ((b1.x-tag.x)*(b1.x-tag.x)+(b1.y-tag.y)*(b1.y-tag.y)>1000) {
                        complete = false;
                    }
                    break;
                case 1:
                    for (let i = 1; i < this.allBlock.length; i++) {
                        if (this.allBlock[i].previous!==this.allBlock[i-1].node) {
                            complete = false;
                            break;
                        }
                    }
                    break;
                case 2:
                    for (let i = 0; i < 3; i++) {
                        if (!this.allBlock[i]) {
                            complete = false;
                            break;
                        }
                        if (i>0 && this.allBlock[i].previous!==this.allBlock[i-1].node) {
                            complete = false;
                            break;
                        }
                    }
                    break;
                default:
                    complete = false;
                    break;
            }
            if (complete) {
                cc.log("通关！");
                this._complete = true;
                setTimeout(() => {
                    let cp = cc.find("Canvas/complete");
                    cp.getComponent("CompleteScript").show();
                }, 1000);
            }
        }
    },
    addBlock(b) {
        if (b) {
            utils.node.setParent(this.node, b, true);
            b.getComponent("BlockScript").father = this.node;
        }
    },
    addMoveBlock(b) {
        if (b) {
            utils.node.setParent(this.node, b, true);
            let bs = b.getComponent("BlockScript");
            bs.father = this.node;
            bs.canDel = true;
            this.setMoveBlock(b);
            if (this.allBlock[bs.index-1]) {
                this.allBlock.push(bs);
            } else {
                this.allBlock[bs.index-1] = bs;
            }
        }
    },
    delMoveBlock() {
        if (this.moveBlock) {
            let bs = this.moveBlock.getComponent("BlockScript");
            while (bs) {
                for (let i = 0; i < this.allBlock.length; i++) {
                    if (this.allBlock[i]===bs) {
                        this.allBlock[i] = null;
                        break;
                    }  
                }
                bs = bs.next?bs.next.getComponent("BlockScript"):null;
            }
            this.moveBlock.destroy();
            this.moveBlock = null;
            for (let i = 3; i < this.allBlock.length; i++) {
                if (this.allBlock[i] && !this.allBlock[this.allBlock[i].index-1]) {
                    this.allBlock[this.allBlock[i].index-1] = this.allBlock[i];
                    this.allBlock[i] = null;
                }  
            }
        }
    }
});
